<?php
namespace App\Admin\Controllers;
use App\Admin\Models\User;
use App\Admin\Models\Withdraw;
use Common\CommonAd;
use Common\ControllerAdBase;

use Common\Models\FWalletsAmountLog;
use Phalcon\Paginator\Adapter\Model as PaginatorModel;

class FinanceController extends ControllerAdBase
{

    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 用户提现列表
     */
    public function withdrawListAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $pageSize = 10;
        } else {
            $pageSize = (int)$this->params['limit'];
        }
        //邀请码
        if ((isset($this->params['invite_code']) && !empty($this->params['invite_code'])) || (isset($this->params['phone']) && !empty($this->params['phone'])) || (isset($this->params['alipay_account']) && !empty($this->params['alipay_account'])) || (isset($this->params['alipay_name']) && !empty($this->params['alipay_name']))) {
            $user = new User();
            $this->params['user_ids'] = $user->search_user($this->params);
        }
        $withdraw = new Withdraw();
        $list_items['items'] = $withdraw->getWithdrawlist($this->params);
        $list_items['total_items'] = $withdraw->count();
        $list_items['money'] = $withdraw->money_totallist();
        $list_items['member_count'] = $withdraw->money_total_count();
        $list_items['adopt_count'] = $withdraw->money_total_yi_count();
        if (!empty($list_items['items'])) {
            $list_items['w_ids'] = implode(',', array_column($list_items['items'], 'id'));
        } else {
            $list_items['w_ids'] = '';
        }
        $this->ok($list_items);
    }

    /**
     * 用户金币提现列表
     */
    public function GoldWithdrawListAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $pageSize = 10;
        } else {
            $pageSize = (int)$this->params['limit'];
        }
        //邀请码
        if ((isset($this->params['invite_code']) && !empty($this->params['invite_code'])) || (isset($this->params['phone']) && !empty($this->params['phone'])) || (isset($this->params['alipay_account']) && !empty($this->params['alipay_account'])) || (isset($this->params['alipay_name']) && !empty($this->params['alipay_name']))) {
            $user = new User();
            $this->params['user_ids'] = $user->search_user($this->params);
        }
        $f_wallet_gold_log = new FWalletGoldLog();
        $list_items = $f_wallet_gold_log->getWithdrawlist($this->params);
        $list_items['money'] = $f_wallet_gold_log->money_totallist();
        if (!empty($list_items['items'])) {
            $list_items['w_ids'] = implode(',', array_column($list_items['items'], 'id'));
        } else {
            $list_items['w_ids'] = '';
        }
        $this->ok($list_items);
    }


    /**
     * 用户提现详情
     */
    public function withdrawdetailAction()
    {
        if (!isset($this->params['id']) && empty($this->params['id'])) {
            $this->fail(-1, '提现id不能为空');
        }
        $withdraw = new Withdraw();
        $withdraw_info = $withdraw->find_by_id($this->params['id']);
        if (empty($withdraw_info)) $this->fail(-1, '没有找到提现记录');
        $member = new Member();
        $member_info = $member->find_by_id($withdraw_info['user_id']);
        if (empty($member_info)) {
            $this->fail(-1, '没有找到用户信息');
        }
        $withdraw_info['phone'] = $member_info['phone'];
        $withdraw_info['nickname'] = $member_info['nickname'];
        $withdraw_info['wx_account'] = $member_info['weixin_no'];
        $withdraw_info['alipay_account'] = $member_info['alipay_account'];

        if ($withdraw_info['status'] == 1) $withdraw_info['status_name'] = '待审核';
        if ($withdraw_info['status'] == 2) $withdraw_info['status_name'] = '已通过';
        if ($withdraw_info['status'] == 3) $withdraw_info['status_name'] = '已拒绝';
        if ($withdraw_info['type'] == 1) $withdraw_info['type_name'] = '支付宝';
        if ($withdraw_info['type'] == 2) $withdraw_info['type_name'] = '微信';
        if ($withdraw_info['is_payed'] == 0) $withdraw_info['is_payed_name'] = '没有到账';
        if ($withdraw_info['is_payed'] == 1) $withdraw_info['is_payed_name'] = '已到账';
        if ($withdraw_info['is_payed'] == -1) $withdraw_info['is_payed_name'] = '失败';
        $this->ok($withdraw_info);
    }


    public function withdrawcheckAction()
    {
        if (!isset($this->params['id']) && empty($this->params['id'])) {
            $this->fail(-1, '提现id不能为空');
        }
        $withdraw = new Withdraw();
        $withdraw_info = $withdraw->find_by_id($this->params['id']);
        if (!isset($this->params['status']) || empty($this->params['status'])) {
            $this->fail(-1, '请选择提现结果');
        }
        if ($this->params['status'] == 3) {
            if (!isset($this->params['tips']) || empty($this->params['tips'])) {
                $this->fail(-1, '不通过的时候，请填写原因');
            }
        }
        $withdraw_info['status'] = $this->params['status'];
        $withdraw_info['tips'] = $this->params['tips'];
        $withdraw_info['check_time'] = date('Y-m-d H:i:s', time());
        $result = $withdraw->save_withdraw($withdraw_info);

        $message = new Messages();
        switch ($this->params['status']) {
            case 2:
                $tips = "您提现的余额{$withdraw_info['money']}元，后台已经审核通过了，系统将在24小时内完成转账。";
                $title = "余额提现后台审核结果";
                break;
            case 3:
                $tips = "您提现的余额{$withdraw_info['money']}元，后台已经审核没有通过，原因:{$withdraw_info['tips']}。";
                $title = "余额提现后台审核结果";
                break;
            default:
                $tips = "该笔{$withdraw_info['money']}元提现审核结果有问题，我们将做进一步处理";
                $title = "余额提现后台审核结果";
                break;
        }
        $message->save_params($withdraw_info['user_id'], $tips, $title);

        if ($result) {
            if ($this->params['push'] == 1) {
                $alias = [$withdraw_info['user_id']];
                $title = '您有新的审核通知';
                $detail = $this->params['status'] == 2 ? "您申请的{$withdraw_info['money']}元提现审核通过了，打款将在两个工作日完成" : "您申请的{$withdraw_info['money']}元体现审核没有通过，原因是：【{$this->params['tips']}】,请快速更正，重新提交申请。";
                $route = [
                    'type' => 1,
                    'path' => 'AwakeMsg',
                    'title' => '路由标题',
                    'url' => '',
                    'data' => '',
                    'notice' => null,
                ];
                $this->jpush->pushmsg_all($alias, $title, $detail, $route);
            }
            $this->ok([]);
        } else {
            $this->fail(-1, '审核失败');
        }
    }


    public function walletdetailAction()
    {
        $Withdraw = new Withdraw();
        $UserMonthIncome = new UserMonthIncome();
        $Wallets = new Wallets();
        $info = $Withdraw->find_by_id($this->params['id']);
        $data = [];
        $data['income'] = $UserMonthIncome->get_all_income($info['user_id']);
        $data['wallets'] = $Wallets->findbyUserMoney($info['user_id']);
        if ($data['wallets'] < 0) {
            $data['is_can_withdraw'] = 0;
            $data['can_can_withdraw'] = 0;
        } else {
            if (round($data['income'] - $data['wallets'], 2) >= $info['money']) {
                $data['is_can_withdraw'] = 1;
                $data['can_can_withdraw'] = $data['income'] - $data['wallets'];
            } else {
                $data['is_can_withdraw'] = 0;
                $data['can_can_withdraw'] = $data['income'] - $data['wallets'];
            }
            $this->ok($data);
        }
        var_dump($data);
        var_dump($info);
        echo 123;
        die;
    }


    /**
     * 余额变更
     */
    public function withdrawUpdateAction(){
        if (!isset($this->params['page']) && empty($this->params['page'])) {
            $this->params['page'] = 1;
        }
        if (!isset($this->params['limit']) && empty($this->params['limit'])) {
            $this->params['limit'] = 10;
        }
        if (!isset($this->params['phone']) && empty($this->params['phone'])) {
            $this->params['phone'] = "";
        }
        $FWalletsAmountLogDb = new FWalletsAmountLog();
        $get_list = $FWalletsAmountLogDb->admin_get_list($this->params);
        $this->ok($get_list);
    }

    public function inviteCodeAction(){
        $this->ok([]);
    }
}
